﻿using System;
using System.Collections.Generic;
using System.Text;

namespace LinWon.Core.DAL
{
    public class DALUser
    {
        private LinWon.Core.Data.SqlHelper helper = LinWon.Core.Data.DataFactory.GetHelper();

        /// <summary>
        /// 获取会员的所有操作员
        /// </summary>
        /// <param name="memberid">会员号</param>
        /// <returns></returns>
        public IList<Model.User> GetUsersByMemberID(int memberid)
        {
            string sql = "SELECT AutoID, MemberID, [Name], Gender, Brithday, Tel, Mobile, Email, IDCode, CreateTime, [State], Remark "
                       + "FROM LW_User WHERE MemberID=@MemberID ";

            System.Data.SqlClient.SqlParameter p = new System.Data.SqlClient.SqlParameter("@MemberID", System.Data.SqlDbType.Int, 4);
            p.Value = memberid;

            System.Data.SqlClient.SqlDataReader dr = null;

            try
            {
                LinWon.Core.Data.SqlHelper helper = LinWon.Core.Data.DataFactory.GetHelper();

                dr = helper.ExecuteReader(sql, p);
                List<Model.User> result = new List<LinWon.Core.Model.User>();
                while (dr.Read())
                {
                    Model.User item = new LinWon.Core.Model.User();
                    item.FillByDataReader(dr);
                    result.Add(item);
                }
                return result;
            }
            finally
            {
                if (null != dr && !dr.IsClosed)
                    dr.Close();
            }
        }

        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="lgName">登录名</param>
        /// <param name="lgPwd">登录密码</param>
        /// <param name="ip">IP</param>
        /// <exception cref="LwException">登录失败的异常</exception>
        /// <returns></returns>
        public Model.User Login(string lgName, string lgPwd, string ip)
        {
            string lgSql = "pLogin";

            System.Data.SqlClient.SqlParameter[] parms = {
                new System.Data.SqlClient.SqlParameter("@LgnName",System.Data.SqlDbType.VarChar,10),
                new System.Data.SqlClient.SqlParameter("@LgnPwd",System.Data.SqlDbType.VarChar,32),
                new System.Data.SqlClient.SqlParameter("LgnIP",System.Data.SqlDbType.VarChar,20)};

            parms[0].Value = lgName;
            parms[1].Value = lgPwd;
            parms[2].Value = ip;

            LinWon.Core.Data.SqlHelper helper = LinWon.Core.Data.DataFactory.GetHelper();

            int result = helper.RunProcReturnInt(lgSql, parms);

            if (result > 0)
                return new LinWon.Core.Model.User(result);
            else
                throw new LwException(result);
        }

        /// <summary>
        /// 物理删除用户
        /// </summary>
        /// <param name="userid">用户号</param>
        /// <returns></returns>
        public int DeleteByPhysical(int userid)
        {
            throw new NotImplementedException("Not Implemented Method");
        }

        /// <summary>
        /// 删除用户(置删除标记)
        /// </summary>
        public int Delete(int id)
        {
            string delete = "UPDATE LW_User SET [State]=@State WHERE AutoID=@AutoID";
            System.Data.SqlClient.SqlParameter[] parms = {
                new System.Data.SqlClient.SqlParameter("@State",System.Data.SqlDbType.Int,4),
                new System.Data.SqlClient.SqlParameter("@AutoID",System.Data.SqlDbType.Int,4)};

            parms[0].Value = Model.UserState.Deleted;
            parms[1].Value = id;

            LinWon.Core.Data.SqlHelper helper = LinWon.Core.Data.DataFactory.GetHelper();

            return helper.ExecuteNonQuery(delete, parms);
        }

        /// <summary>
        /// 用户状态置为正常(解锁、还原)
        /// </summary>
        /// <param name="userid">用户号</param>
        /// <returns></returns>
        public int Recover(int userid)
        {
            string sql = "UPDATE LW_User SET [State]=@State WHERE AutoID=@AutoID";
            System.Data.SqlClient.SqlParameter[] parms ={
                new System.Data.SqlClient.SqlParameter("@State", System.Data.SqlDbType.Int,4),
                new System.Data.SqlClient.SqlParameter("@AutoID", System.Data.SqlDbType.Int,4)};

            parms[0].Value = Model.UserState.Normal;
            parms[1].Value = userid;

            LinWon.Core.Data.SqlHelper helper = LinWon.Core.Data.DataFactory.GetHelper();

            return helper.ExecuteNonQuery(sql, parms);
        }

        /// <summary>
        /// 锁定用户
        /// </summary>
        /// <param name="userid">用户号</param>
        /// <returns></returns>
        public int Lock(int userid)
        {
            string sql = "UPDATE LW_User SET [State]=@State WHERE AutoID=@AutoID";
            System.Data.SqlClient.SqlParameter[] parms ={
                new System.Data.SqlClient.SqlParameter("@State", System.Data.SqlDbType.Int,4),
                new System.Data.SqlClient.SqlParameter("@AutoID", System.Data.SqlDbType.Int,4)};

            parms[0].Value = Model.UserState.Locked;
            parms[1].Value = userid;

            LinWon.Core.Data.SqlHelper helper = LinWon.Core.Data.DataFactory.GetHelper();

            return helper.ExecuteNonQuery(sql, parms);
        }        
    }
}
